<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    ChinaWanderer
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: UsersController.php 620 2011-05-03 02:23:14Z ma.rongliang $$
 * @copyright   Copyright (c) 2011 Evebit Inc. China (http://www.evebit.com)
*/


/**
 * users controller for backend
 * 
 * view,delete,search,sort,edit users
 * 
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: UsersController.php 620 2011-05-03 02:23:14Z ma.rongliang $$
 */

class User_IndexController extends Evebit_Controller_Action_Backend {
	
	/**
	 * @var $_controllerUrl string
	 */
	private $_controllerUrl = 'index';
	/**
	 * @var $_moduleUrl string
	 */
	private $_moduleUrl = 'user';
	
	/**
	 * default action of the users controller
	 * 
	 * view the users
	 * 
	 */
	public function indexAction() {				
		$session = $this->getSession();
		$session->initGrid('users', 
			array(
				'id'=> array('fields'=> 'id', 'type'=> 'int'), 
				'username'=> array('fields'=> 'username', 'type'=> 'string'), 
				'email'=> array('fields'=> 'email', 'type'=> 'string'), 
			    'role'=> array('fields'=> 'select_role', 'type'=> 'string')),
			array(
				'id'=> 'id', 
				'username'=> 'username', 
				'email'=> 'email', 
				'action'=> $this->__('Action')), 
			array('size'=> 10, 'sort'=> 'id', 'order'=> 'asc'));	
		$admin = new User_Model_Entity_User();
		$collection = $admin->getCollection();
		$adapter = new Evebit_Model_Paginator_Adapter($collection);
		$paginator = new Zend_Paginator($adapter);
		$session->applyCollection($collection, $paginator);
		$this->view->collection = $collection;
		$this->view->paginator = $paginator;
		$this->view->state = $session->getGridState();
	}
	
	/**
	 * reset action of the users controller
	 * 
	 * reset the users of the grid
	 * 
	 */
	public function resetAction() {
		$this->getSession()->resetGrid();
		$this->_forward('index', $this->_controllerUrl);
	}
	
	/**
	 * add action of the users controller
	 * 
	 * add a user
	 * 
	 */
	public function addAction() {
		$this->_forward ( 'edit', null, null, array ('id' => 0 ) );
	}
	
	/**
	 * delete action of the users controller
	 * 
	 * delete one or more users
	 * 
	 */
	public function deleteAction() {
		$ids = $this->getRequest()->getParam('id');
		if (count($ids) > 0) {
			foreach ($ids as $id) {
				try {
					$user = new User_Model_Entity_User();
					$user->load($id);
					if ($user->getId() != $id) {
						throw new Exception("");
					}
					$user->delete();
				} catch (Exception $e) {
					$this->getSession()->addError(
						$this->__("User %d not found or deleted failed", $id));
				}
			}
			$this->getSession()->addSuccess($this->__('Delete Success'));
		} else {
			$this->getSession()->addNotice($this->__('Please select a record'));
		}
		$this->_redirect($this->_controllerUrl);
	}
	
	/**
	 * edit action of the users controller
	 * 
	 * edit a user,if the user is exist,show the user,
	 * else show a blank page to add a user
	 * 
	 */
	public function editAction() {
		$id = $this->getRequest()->getParam('id');
		
		$user = new User_Model_Entity_User();
		$user->load($id);
		
		$title = $user->id?'Edit User':'Add new User';
		
		$this->view->user = $user;
		$this->view->title = $title;
		$this->view->fieldsets = $user->getFormField('user');
	}
	
	/**
	 * saveEdit action of the users controller
	 * 
	 * when an administrator edited the user,save it
	 * 
	 */
	public function saveEditAction() {
		$user = new User_Model_Entity_User();
		$data = $this->getRequest()->getPost('data');
		if (empty($data['password']))
		    unset($data['password']);
		$user->addData($data);
		if (!$user->id && $user->id !== 0){
			$user->id = null;
		}
		try {
			$user->save();
		} catch (Exception $e) {
			$this->getSession()->addError($this->__('Register failed'));
			return $this->_forward('edit', $this->_controllerUrl, $this->_moduleUrl, array("id"=> $data["id"]));
		}
		$this->getSession()->addSuccess($this->__('Save Success'));
		$this->_redirect($this->_controllerUrl);
	}
	
}

